package com.example.springboot.service.Impl;

import com.example.springboot.entity.Post;
import com.example.springboot.entity.vo.PostVo;
import com.example.springboot.mapper.PostMapper;
import com.example.springboot.service.ESService;
import com.example.springboot.service.PostService;
import com.example.springboot.service.UserService;
import com.example.springboot.utils.CommonUtil;
import com.example.springboot.utils.RedisUtil;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class PostServiceImpl implements PostService {
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ESService esService;
    @Override
    public List<PostVo> findAll(Integer offset, Integer pageSize) {
        List<Post> posts = postMapper.findAll(offset, pageSize);
        List<PostVo> res = new ArrayList<>();

        for (Post post : posts) {
            PostVo postVo = new PostVo();
            try {
                BeanUtils.copyProperties(post, postVo);
                postVo.setNickname(userService.getUserById(post.getAuthorId()).getNickname());
                res.add(postVo);
            } catch (Exception e){
                System.out.println(e);
            }
        }
        return res;
    }

    @Override
    public List<PostVo> findAll(Integer offset, Integer pageSize, Integer categoryId) {
        List<PostVo> res = new ArrayList<>();
        try {
            res=esService.getDataByCategoryId(categoryId, offset, pageSize);
        } catch (Exception e1) {
            System.out.println(e1);
            try {
                List<Post> posts = postMapper.findAllByCategoryId(offset, pageSize, categoryId);
                for (Post post : posts) {
                    PostVo postVo = new PostVo();
                    BeanUtils.copyProperties(post, postVo);
                    postVo.setNickname(userService.getUserById(post.getAuthorId()).getNickname());
                    res.add(postVo);
                }
            } catch (BeansException e) {
                throw new RuntimeException(e);
            }
        }finally {
            return res;
        }
    }

    @Override
    public void insert(Post post) {
        post.updateUpdatedTime();
        post.updatePublishedTime();
        postMapper.insert(post);
    }

    @SneakyThrows
    @Override
    public Post findOneById(Integer postId) {
        String key = "blog_post:post:" + postId;
        //先去redis找
        Post post = new Post();
        try {
            Map<Object,Object> map= redisUtil.hmget(key);
            post =CommonUtil.mapToPost(map);
        } catch (Exception e) {
            System.out.println(e);
            post = postMapper.findOneById(postId);
            return post;
        }

        if (post.getId() == null) {
            // 如果 Redis 中找不到，则从数据库中查询
            post = postMapper.findOneById(postId);
            if (post != null) {
                // 将查询到的数据存入 Redis
                Map<String, Object> map = CommonUtil.postToMap(post);
                redisUtil.hmset(key, map);
                redisUtil.expire(key, 60 * 60 * 24);
            }
        }

        return post;
    }

    @Override
    public List<PostVo> getRankingsByType(String type) {
        //TODO 先用获取所有数据占位,后面要改的

        List<Post> posts = new ArrayList<>();
        switch (type){
            case "likes":
                posts = postMapper.getRankingsByLikes(0,100);
                break;
            case "views":
                posts = postMapper.getRankingsByViews(0,100);
                break;
            case "collectCount":
                posts = postMapper.getRankingsByCollectCount(0,100);
                break;
            default:
                return null;
        }
        List<PostVo> res = new ArrayList<>();

        for (Post post : posts) {
            PostVo postVo = new PostVo();
            try {
                BeanUtils.copyProperties(post, postVo);
                postVo.setNickname(userService.getUserById(post.getAuthorId()).getNickname());
                res.add(postVo);
            } catch (Exception e){
                System.out.println(e);
            }
        }
        return res;
    }

    @Override
    public void addViews(Integer id) {
        postMapper.increViews(id);
    }
}
